﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERPTOOLS.Class;

namespace ERPTOOLS
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        //    this.Visible = true;
        //    Check tr = new Check();
        //    //Main_Run mr = new Main_Run(tr);
        //    tr.Show();
        }

        private void login_Load(object sender, EventArgs e)
        {
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode").ToString() != "")
            {
                textBox1.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode").ToString();
                textBox1.ReadOnly = true;
            }
            else
            {
                textBox1.ReadOnly = false;
            }

            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyName").ToString() != "")
            {
                textBox2.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyName").ToString();
            }
            else
            {
                textBox2.ReadOnly = true;
            }
            
        }

        private void lbldbtest_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DBCon dbcon = new DBCon();
            dbcon.Show();
        }

        private void btcancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btconfirm_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("公司的编号或者登录名和密码不能为空");
                return;
            }

            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//DBIP") == "" || XMLHelper.GetInnerTextByXpath("Configure.xml", "//DBUser") == ""
                || XMLHelper.GetInnerTextByXpath("Configure.xml", "//DBPassword") == "" || XMLHelper.GetInnerTextByXpath("Configure.xml", "//Database") == "")
            {
                MessageBox.Show("请先配置本地DB连接设置");
                return;
            }
            if (!TestInfo())
            {
                return;
            }
            XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml","//Login","CompanyCode",textBox1.Text);
            XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml","//Login","CompanyName",textBox2.Text);
            XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Login", "User", textBox3.Text);
            //XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Login", "Password", textBox4.Text);
            this.Visible=false;
            Check ck = new Check();
            ck.Show();
            
        }

        private bool TestInfo()
        {
            bool bl1=true;
            bool bl2=true;
            DataTable dt= DBLink.LinkSCM("select MA001 from SYSMA where MA001='" + textBox1.Text + "'", "company");
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("公司编号不存在，请重新填写。");
                bl1= false;
                return bl1;
            }

            dt = DBLink.LinkSCM("select MB002 from SYSMB where MB001='" + textBox1.Text + "' and MB002='" + textBox3.Text + "' and MB011='" + textBox4.Text + "'", "user");
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("您输入的用户名密码不正确，请重新输入。");
                bl2 = false;
                return bl2;
            }
            return (bl1 & bl2);
        }
    }
}
